package lc1993929.com.secret.atys;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.List;

import lc1993929.com.secret.Config;
import lc1993929.com.secret.R;
import lc1993929.com.secret.ld.MyContacts;
import lc1993929.com.secret.net.Message;
import lc1993929.com.secret.net.TimeLine;
import lc1993929.com.secret.net.UploadContacts;
import lc1993929.com.secret.utils.MD5Tools;

/**
 * Created by LiuChang on 2017/5/8.
 */

public class AtyTimeLine extends AppCompatActivity {

    private String phone_num, phone_md5, token;
    private AtyTimeLineMessageListAdapter adapter;
    private ListView timelineListView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aty_timeline);

        phone_num = getIntent().getStringExtra(Config.KEY_PHONE_NUM);
        token = getIntent().getStringExtra(Config.KEY_TOKEN);
        phone_md5 = MD5Tools.MD5(phone_num);
        adapter = new AtyTimeLineMessageListAdapter(this);

        timelineListView = (ListView) findViewById(R.id.timelineList);
        timelineListView.setAdapter(adapter);
        timelineListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Message message = adapter.getItem(position);
                Intent intent = new Intent(AtyTimeLine.this, AtyMessage.class);
                intent.putExtra(Config.KEY_MSG, message.getMessage());
                intent.putExtra(Config.KEY_MSG_ID, message.getMessageId());
                intent.putExtra(Config.KEY_PHONE_MD5, message.getPhone_md5());
                intent.putExtra(Config.KEY_TOKEN, token);
                startActivity(intent);
            }
        });

        final ProgressDialog pd = ProgressDialog.show(this, getResources().getString(R.string.connecting), getResources().getString(R.string.connecting_to_server));
        new UploadContacts(phone_md5, token, MyContacts.getContactsJSONString(this), new UploadContacts.SuccessCallback() {
            @Override
            public void onSuccess() {
                loadMessage();

                pd.dismiss();
            }
        }, new UploadContacts.FailCallback() {
            @Override
            public void onFail(int errorCode) {
                pd.dismiss();
                if (errorCode == Config.RESULT_STATUS_INVALID_TOKEN) {
                    startActivity(new Intent(AtyTimeLine.this, AtyLogin.class));
                    finish();
                } else {
                    loadMessage();
                }
            }
        });
    }

    private void loadMessage() {
        final ProgressDialog pd = ProgressDialog.show(this, getResources().getString(R.string.connecting), getResources().getString(R.string.connecting_to_server));

        new TimeLine(phone_md5, token, 1, 20, new TimeLine.SuccessCallback() {

            @Override
            public void onSuccess(int page, int perpage, List<Message> timeLine) {
                pd.dismiss();

                adapter.clear();
                adapter.addAll(timeLine);
            }
        }, new TimeLine.FailCallback() {

            @Override
            public void onFail(int errorCode) {
                pd.dismiss();

                if (errorCode == Config.RESULT_STATUS_INVALID_TOKEN) {
                    startActivity(new Intent(AtyTimeLine.this, AtyLogin.class));
                    finish();
                } else {
                    Toast.makeText(AtyTimeLine.this, R.string.fail_to_load_timeline_data, Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_aty_timeline, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menuShowAtyPublish:
                Intent intent = new Intent(AtyTimeLine.this, AtyPublish.class);
                intent.putExtra(Config.KEY_PHONE_MD5, phone_md5);
                intent.putExtra(Config.KEY_TOKEN, token);
                startActivityForResult(intent, 0);
                break;
            default:
                break;
        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            case Config.ACTIVITY_RESULT_NEED_REFRESH:
                loadMessage();
                break;
            default:
                break;
        }
    }
}
